//
//  QCSegmentedView.swift
//  SegmentedView
//
//  Created by lizhi on 2021/12/10.
//

import UIKit


/// 左右标签 滑动View
class QCSegmentedView: UIView {

    
    public weak var listContainer: QCSegmentedListContainerView? = nil {
        didSet {
//            listContainer?.defaultSelectedIndex = defaultSelectedIndex
//            contentScrollView = listContainer?.contentScrollView()
        }
    }
    
    open weak var dataSource: QCSegmentedTitleDataSource? {
        didSet {
//            dataSource?.reloadData(selectedIndex: selectedIndex)
        }
    }
    
    public private(set) var  collectionView:UICollectionView!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setup()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setup()  {
        setupCollectionView()
    }
    func setupCollectionView()  {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.estimatedItemSize = CGSize(width: 60, height: 30)
        collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .clear
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.scrollsToTop = false
        collectionView.dataSource = self
        collectionView.delegate = self
        
        collectionView.register(QCSegmentedCollectionCell.self, forCellWithReuseIdentifier: "QCSegmentedCollectionCell")
        self.addSubview(collectionView)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.collectionView.snp.makeConstraints {
            $0.height.equalTo(30)
            $0.centerY.equalToSuperview()
            $0.leading.trailing.equalToSuperview()
//            $0.edges.equalToSuperview()
        }
    }
}

extension QCSegmentedView: UICollectionViewDataSource,UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 15
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "QCSegmentedCollectionCell", for: indexPath) as!  QCSegmentedCollectionCell
        let cellStr = "cell 123  \(indexPath.row)"
        cell.titleLabel.text = cellStr
        cell.layoutIfNeeded()
        return cell
    }
    
    
}
